Ubuntu18.04设置开机启动脚本

您所在的位置:网站首页 ubuntu 添加开机启动脚本 Ubuntu18.04设置开机启动脚本

Ubuntu18.04设置开机启动脚本

2022-08-16 18:55| 来源: 网络整理| 查看: 265

目录

1.开机自启动脚本方法一

2.开机自启动脚本方法二

2.1.修改/etc/systemd/system/rc-local.service,在后面增加install区块.

2.2.创建并修改rc.local文件

2.3.测试

1.开机自启动脚本方法一

在/etc/init.d中创建shell脚本

cd /etc/init.d vim test.sh

在脚本中输入如下内容:

#!/bin/bash   #以下为你想开机执行的内容,也可以是执行一个脚本,例如 ./auto.sh sudo sh -c "echo 150 > /sys/devices/pwm-fan/target_pwm"   exit 0

然后给脚本增加可执行权限,

chmod +750 test.sh

然后设置开机自动启动,在这里90表明一个优先级,越高表示执行的越晚

sudo update-rc.d test.sh defaults 90

然后重启进行测试。

如想移除开机启动,执行如下命令:

update-rc.d -f test.sh remove 2.开机自启动脚本方法二 2.1.修改/etc/systemd/system/rc-local.service,在后面增加install区块. sudo vim /etc/systemd/system/rc-local.service

打开文件如下:

# SPDX-License-Identifier: LGPL-2.1+ # # This file is part of systemd. # # systemd is free software; you can redistribute it and/or modify it # under the terms of the GNU Lesser General Public License as published by # the Free Software Foundation; either version 2.1 of the License, or # (at your option) any later version. # This unit gets pulled automatically into multi-user.target by # systemd-rc-local-generator if /etc/rc.local is executable. [Unit] Description=/etc/rc.local Compatibility Documentation=man:systemd-rc-local-generator(8) ConditionFileIsExecutable=/etc/rc.local After=network.target [Service] Type=forking ExecStart=/etc/rc.local start TimeoutSec=0 RemainAfterExit=yes GuessMainPID=no

配置文件解析(主要分成三个部分):

[Unit] 段: 启动顺序与依赖关系 [Service] 段: 启动行为,如何启动,启动类型 [Install] 段: 定义如何安装这个配置文件,即怎样做到开机启动

可以看出,/etc/rc.local 的启动顺序是在网络后面,但是显然它少了 Install 段,也就没有定义如何做到开机启动,所以显然这样配置是无效的。 因此我们就需要在后面帮他加上 [Install] 段:

[Install] WantedBy=multi-user.target Alias=rc-local.service

修改后的文件内容如下:

# SPDX-License-Identifier: LGPL-2.1+ # # This file is part of systemd. # # systemd is free software; you can redistribute it and/or modify it # under the terms of the GNU Lesser General Public License as published by # the Free Software Foundation; either version 2.1 of the License, or # (at your option) any later version. # This unit gets pulled automatically into multi-user.target by # systemd-rc-local-generator if /etc/rc.local is executable. [Unit] Description=/etc/rc.local Compatibility Documentation=man:systemd-rc-local-generator(8) ConditionFileIsExecutable=/etc/rc.local After=network.target [Service] Type=forking ExecStart=/etc/rc.local start TimeoutSec=0 RemainAfterExit=yes GuessMainPID=no [Install] WantedBy=multi-user.target Alias=rc-local.service 2.2.创建并修改rc.local文件 sudo vim /etc/rc.local

在里面添加如下内容,注意第一行要用bash,不要用sh:

#!/bin/bash -e # # rc.local # # This script is executed at the end of each multiuser runlevel. # Make sure that the script will "exit 0" on success or any other # value on error. # # In order to enable or disable this script just change the execution # bits. # # By default this script does nothing. #这里就是我自己添加的启动脚本 bash /data/chw/temp/auto.sh exit 0

加入系统程序中:

sudo systemctl enable rc-local#这条语句就是创建一个超链接,在系统启动服务程序中.

给文件赋予可执行权限

sudo chmod +x /etc/rc.local 2.3.测试

我的/data/chw/temp/auto.sh脚本里面的内容是:

#!/bin/bash cd /data/chw/temp python main.py > ./log.txt

然后我的main.py里面的内容是:

# -*- coding:utf-8 -*- import sys while 1: print("this is a auto sh,") print("==================")

如果想不重启,直接查看效果执行如下命令即可,启动服务并检查状态,如果修改了rc-local.service文件,则需要用sudo systemctl daemon-reload重新加载.:

sudo systemctl daemon-reload sudo systemctl stoprc-local.service sudo systemctl start rc-local.service sudo systemctl status rc-local.service

如果想重启看下效果:

sudo init 6#重启命令 sudo reboot -h now #重启命令

参考文献:

ubuntu18.04中如何设置开机启动脚本,开机启动命令  https://blog.csdn.net/xiaoxiao_yingzi/article/details/100520171

ubuntu18.04 设置开机启动脚本  https://www.jianshu.com/p/85039842d318



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3